import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl yday -o yday.json
https://kag0yy3r.ynofboeo.com/
'''


#     item['tags'] = tags
#     item['urls'] = url
#     item['names'] = name
#     item['picture_url'] = pUrl
#     item['video_url'] = vUrl

class YdaySpider(scrapy.Spider):
    name = "yday"
    # 前缀
    prefix = 'https://kag0yy3r.'
    # 中缀
    website = 'ynofboeo'
    # 后缀
    suffix = '.com/'
    allowed_domains = [website + '.com']
    # start_urls = [prefix + website + suffix+'gqck.html']
    start_urls = ["https://kag0yy3r.ynofboeo.com/gqck.html"]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        tags = response.xpath("//h3[@class='title']/text()").extract()
        video_url = get_video_url_one(content)
        urls = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@ href").extract()
        names = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@ title").extract()
        picture_url = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@ data-original").extract()
        if len(tags) and len(urls) and len(names) and len(picture_url):
            for i in range(len(picture_url)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[-1].strip(), url=split_joint(self.prefix + self.website + self.suffix, urls[i]), name=names[i], pUrl=picture_url[i], vUrl='')
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, url=response.url, name='', pUrl='', vUrl=format_url_one(video_url[0]))
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
